package cn.tedu.smartos.activity.controller;

import cn.tedu.smartos.activity.pojo.dto.*;
import cn.tedu.smartos.activity.pojo.entity.ActivityCategory;
import cn.tedu.smartos.activity.pojo.vo.ActivityCategoryVO;
import cn.tedu.smartos.activity.pojo.vo.ActivityVO;
import cn.tedu.smartos.activity.service.ActivityService;
import cn.tedu.smartos.base.exception.ServiceException;
import cn.tedu.smartos.base.response.JsonResult;
import cn.tedu.smartos.base.response.StatusCode;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "03.活动模块")
@Slf4j
@RestController
@RequestMapping("/api/v1/activities/")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @ApiOperation("所有活动列表")
    @ApiOperationSupport(order = 10)
    @GetMapping("list")
    public JsonResult list(ActivityQuery query) {
        log.debug("开始处理【所有活动列表】的请求,参数{}", query);
        PageInfo<ActivityVO> activityList = activityService.list(query);
        return JsonResult.ok(activityList);
    }

    @ApiOperation("我的活动列表")
    @ApiOperationSupport(order = 15)
    @GetMapping("myActivities")
    public JsonResult myActivities(@RequestParam String username) {
        log.debug("开始处理【我的活动列表】的请求,参数{}", username);
        List<ActivityVO> myActivities = activityService.getMyActivities(username);
        return JsonResult.ok(myActivities);
    }

    @ApiOperation(value = "活动详情")
    @ApiOperationSupport(order = 20)
    @GetMapping("detail")
    public JsonResult detail(@RequestParam Long activityId) {
        log.debug("开始处理【活动详情】的请求,参数{}", activityId);
        ActivityVO activityVO = activityService.detail(activityId);
        return JsonResult.ok(activityVO);
    }
    @ApiOperation(value = "删除活动")
    @ApiOperationSupport(order = 30)
    @PostMapping("delete")
    public JsonResult delete(@RequestBody List<Long> activityIds) {
        log.debug("开始处理【删除活动】的请求,参数{}", activityIds);
        activityService.delete(activityIds);
        return JsonResult.ok();
    }

    @ApiOperation("活动修改(包括审核活动功能)")
    @ApiOperationSupport(order = 40)
    @PostMapping("update")
    public JsonResult update(@RequestBody ActivityUpdateParam updateParam) {
        log.debug("开始处理【活动修改】的请求,参数{}", updateParam);
        activityService.update(updateParam);
        return JsonResult.ok();
    }

    @ApiOperation("发布活动")
    @ApiOperationSupport(order = 50)
    @PostMapping("publish")
    public JsonResult publish(@RequestBody ActivityAddParam addParam) {
        log.debug("开始处理【发布活动】的请求,参数{}", addParam);
        activityService.publish(addParam);
        return JsonResult.ok();
    }

    @ApiOperation("管理员查看待审核活动")
    @ApiOperationSupport(order = 60)
    @GetMapping("waitAudit")
    public JsonResult waitAudit(ActivityQuery query) {
        log.debug("开始处理【管理员查看待审核活动】的请求,参数{}", query);
        PageInfo<ActivityVO> pageInfo = activityService.waitAudit(query);
        return JsonResult.ok(pageInfo);
    }

    @ApiOperation("添加活动分类")
    @ApiOperationSupport(order = 80)
    @PostMapping("category/add")
    public JsonResult addCategory(@RequestBody ActivityCategory activityCategory) {
        log.debug("开始处理【添加活动分类】的请求,参数{}", activityCategory);
        activityService.addCategory(activityCategory);
        return JsonResult.ok();
    }

    @ApiOperation("删除活动分类")
    @ApiOperationSupport(order = 90)
    @PostMapping("category/delete")
    public JsonResult deleteCategory(@RequestBody List<Integer> categoryIds) {
        log.debug("开始处理【删除活动分类】的请求,参数{}", categoryIds);
        activityService.deleteCategory(categoryIds);
        return JsonResult.ok();
    }

    @ApiOperation("修改活动分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("category/update")
    public JsonResult updateCategory(@RequestBody ActivityCategory activityCategory) {
        log.debug("开始处理【修改活动分类】的请求,参数{}", activityCategory);
        activityService.updateCategory(activityCategory);
        return JsonResult.ok();
    }

    @ApiOperation("活动分类列表")
    @ApiOperationSupport(order = 110)
    @GetMapping("category/list")
    public JsonResult listCategory(ActivityQuery query) {
        log.debug("开始处理【活动分类列表】的请求,参数{}", query);
        PageInfo<ActivityCategoryVO> categoryList = activityService.listCategory(query);
        return JsonResult.ok(categoryList);
    }

    @ApiOperation("报名活动")
    @ApiOperationSupport(order = 200)
    @PostMapping("join")
    public JsonResult join(@RequestBody ParticipationAddParam participation) {
        log.debug("开始处理【报名活动】的请求,参数:{}", participation);
        activityService.join(participation);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 210)
    @PostMapping("checkin")
    @ApiOperation("活动签到")
    public JsonResult checkIn(@RequestBody ParticipationAddParam participation) {
        log.debug("开始处理【活动签到】的请求,参数:{}", participation);
        try {
            return activityService.checkIn(participation);
        } catch (Exception e) {
            log.error("签到系统异常", e);
            return new  JsonResult(StatusCode.FAILED, "系统异常，签到失败");
        }
    }
    @ApiOperation("活动签退")
    @ApiOperationSupport(order = 220)
    @PostMapping("checkout")
    public JsonResult checkout(@RequestBody ParticipationCheckoutParam participation) {
        log.debug("开始处理【活动签退】的请求,参数:{}", participation);
        activityService.checkout(participation);
        return JsonResult.ok();
    }
    @ApiOperation("确认活动排班")
    @ApiOperationSupport(order = 230)
    @PostMapping("confirmSchedule")
    public JsonResult confirmSchedule(@RequestParam Long scheduleId, @RequestParam Long userId) {
        log.debug("开始处理【确认活动排班】的请求,参数:{},{}", scheduleId, userId);
        activityService.confirmSchedule(scheduleId, userId);
        return JsonResult.ok();
    }
}